package net.sapfree.display.tags.course;

import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.jsp.JspException;

import net.sapfree.common.Constants;
import net.sapfree.core.db.DBConnectionProvider;
import net.sapfree.core.db.DataBaseExecutor;
import net.sapfree.core.db.Record;
import net.sapfree.core.db.RecordSet;
import net.sapfree.display.tags.html.LinkTag;

public class CourseTag extends LinkTag{
	private String quanuuid;//班级uuid
	private String weekday; //星期
	private String courseindex; //节次
	private String halfday; //上下午区分
	private String id;
	private Record r=null;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public int doStartTag() throws JspException {
		
		
		if(quanuuid==null||"".equals(quanuuid)){
			CourseListTag artList = (CourseListTag) pageContext.getAttribute("CourseListTag");
			r=(Record)artList.getCurrent();
		}else{
			Connection conn=null;
			try {
				conn = DBConnectionProvider.getConnection(Constants.JNDI_SAPFREE);
				DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
				RecordSet courseList = new RecordSet();
				courseList=dbe.find(CourseSql());
				if(courseList!=null&&courseList.size()>0){
					r=courseList.get(0);
				}
			} catch (SQLException e) {
				log.error("查询课程时出错",e);
			}finally{
				try{
					 conn.close();
				}catch(Exception e){}
			}
		}
		
		pageContext.setAttribute("CourseTag", this);
		return EVAL_BODY_INCLUDE;
	}
	
	
	private String CourseSql(){
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT * FROM COURSESCHEDULE,COURSEDETAIL " );
		sql.append(" WHERE CS_UUID=CD_CSUUID AND CS_CLASSUUID='").append(quanuuid).append("'");
		if(weekday!=null && !"".equals(weekday)){
			sql.append(" AND CD_WEEKDAY=").append(weekday);
		}
		if(courseindex!=null && !"".equals(courseindex)){
			sql.append(" AND CD_INDEX=").append(courseindex);
		}
		if(halfday!=null && !"".equals(halfday)){
			sql.append(" AND CD_HALFDAY='").append(halfday).append("'");
		}
		return sql.toString();
	}
	
	
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	/**
	 * @return the quanuuid
	 */
	public String getQuanuuid() {
		return quanuuid;
	}

	/**
	 * @param artName the artName to set
	 */
	public void setQuanuuid(String quanuuid) {
		this.quanuuid = quanuuid;
	}
	
	public String getWeekday(){
		return weekday;
	}
	public void setWeekday(String weekday){
		this.weekday = weekday;
	}
	
	public String getCourseindex(){
		return courseindex;
	}
	public void setCourseindex(String courseindex){
		this.courseindex = courseindex;
	}
	
	public void setHalfday(String halfday){
		this.halfday = halfday;
	}
	public String getHalfday(){
		return this.halfday;
	}
	
	
	public void setId(String id){
		this.id = id;
	}
	public String getId(){
		return this.id;
	}
	/**
	 * @return the artName
	 */
	public Record getRecord() {
		return r;
	}

	/**
	 * @param artName the artName to set
	 */
	public void setRecord(Record _r) {
		this.r = _r;;
	}
	
}
